<template>
  <div
    class="blogcontent"
    :style="{ backgroundImage: 'url(' + content.coverIamge + ')' }"
  >
    <!-- 中间主题 -->
    <div class="blogmain">
      <h1><i class="el-icon-reading"></i>{{ content.title }}</h1>
      <div class="createtime">最后更新日期 {{ content.createTime }}</div>
      <!-- 文章内容 -->
      <div v-html="content.content"></div>

      <!-- 底部页签 -->
      <div class="bottomtab">
        <div class="changeBlog" @click="goToBack">返回</div>
        <div class="changeBlog" @click="updatBlog">编辑</div>
        <div class="changeBlog" @click="deleteBlog">删除</div>
      </div>
    </div>

    <!-- 返回顶部 -->
  </div>
</template>   
<script>
// 查询数据库内容的方法
import { getCoolePage, removeColl } from "../../util/time";
export default {
  data() {
    return {
      // 文章id
      blogid: "",
      // 文章内容
      content: "",
    };
  },
  mounted() {
    // 获取路由中的id信息
    this.blogid = this.$router.history.current.query.id;
    // 根据id获取文章详情
    this.getBlogDetail();
  },
  methods: {
    // 根据id获取文章详情
    getBlogDetail() {
      getCoolePage("myBlogList", 1, 1, {
        _id: this.blogid,
      }).then((res) => {
        console.log(res);
        this.content = res.data[0];
        this.$nextTick(() => {
          this.settingPreStyle();
        });
      });
    },
    // 动态设置
    settingPreStyle() {
      let pernodes = document.querySelectorAll("pre");
      pernodes.forEach((item) => {
        item.style.padding = "20px";
        item.style.backgroundColor = "black";
        item.style.color = "white";
      });
      this.$forceUpdate();
    },
    // 返回上一页
    goToBack() {
      this.$router.go(-1);
    },
    // 删除
    deleteBlog() {
      // 提示添加成功
      this.$confirm("此操作将永久删除该文章, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          removeColl("myBlogList", this.content._id).then((res) => {
            if (res.deleted > 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.goToBack();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 编辑
    updatBlog() {
      this.$router.push({ path: "/personal/addblog?id=" + this.content._id });
    },
  },
};
</script>

<style scoped>
.blogcontent {
  background-color: white;
  display: flex;
  justify-content: center;
  position: absolute;
  height: 87vh;
  width: 100%;
  background-size: 100% 100%;
  overflow-y: hidden;
}
.blogmain {
  width: 80%;
  max-height: 100%;
  overflow: auto;
  background-color: white;
  opacity: 0.8;
  backdrop-filter: blur(4px);
  padding: 10px;
}
.bottomtab {
  border-top: 1px solid #585858;
  padding-top: 10px;
  height: 88px;
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  font-size: 14px;
}
.changeBlog:hover {
  color: #066e91;
  cursor: pointer;
}
.createtime {
  text-align: right;
  padding-right: 15px;
  color: #999;
  font-size: 13px;
}

@media screen and (max-width: 414px) {
  .blogmain {
    width: 100%;
    max-height: 100%;
    overflow: auto;
    background-color: white;
    opacity: 0.8;
    backdrop-filter: blur(4px);
    padding: 10px;
  }
}
</style>